from PyQt5.QtCore import *
import sys, time, os, psutil, webbrowser,threading
from PyQt5.QtWidgets import QMainWindow, QLabel, QPushButton, QFrame, QApplication
from PyQt5.QtCore import QRect
from PyQt5.QtGui import QPixmap

global num_cpu, num_mem

num_cpu = 0
num_mem = 0


class Ui_Form(QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_ui()
    def init_ui(self):
        self.setWindowFlags(Qt.FramelessWindowHint)  # 去边框
        self.setAttribute(Qt.WA_TranslucentBackground)  # 设置窗口背景透明
        self.resize(602, 507)

        self.frame = QFrame(self)
        self.frame.setGeometry(QRect(99, 130, 51, 51))
        self.frame.setStyleSheet("background-color: qconicalgradient(cx:0.5, cy:0.5, angle:90, stop:0.245487 rgba(85, 170, 255, 255), stop:0.249097 rgba(255, 255, 255, 255));\n"
"border-radius:25px;")
        self.frame.setFrameShape(QFrame.StyledPanel)
        self.frame.setFrameShadow(QFrame.Raised)
        self.frame.setObjectName("frame")
        self.retranslateUi()

    def retranslateUi(self):
        self.timer = QTimer()
        self.timer.timeout.connect(self.progress)
        self.timers = 0
        self.timer.start(15)
        self.start_fresh()
    def start_fresh(self):
        global num_cpu, num_mem
        num_mem = int(psutil.virtual_memory().percent)
        num_cpu = int(psutil.cpu_percent())

    def progress(self):
        def progressBar_change(value):
            index2 = str(float(100 - value) / 100 - 0.001)
            index = str(float(100 - value) / 100)
            style = """
            QFrame{
            border-radius:25px;
            background-color: qconicalgradient(cx:0.5, cy:0.5, angle:90, stop:index rgba(0, 0, 0, 0), stop:index2 rgba(120, 161, 255, 255));
            }
            """
            newstyle = style.replace("index", index).replace("index2", index2)
            self.frame.setStyleSheet(newstyle)
        print(self.timers)
        self.timers = self.timers + 1
        global num_mem
        if self.timers > num_mem:
            self.timer.stop()
        else:
            progressBar_change(self.timers)
            self.label_5.setText(str(num_mem) + "%")


def main():
    app = QApplication(sys.argv)
    gui = Ui_Form()
    gui.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
